<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="./static/css/bootstrap.min.css">

</head>

<body>
    <!-- 给一个容器 -->
    <div class="container">
        <div class="row text-center">
            <h1>学生信息管理系统</h1>
            <hr>
            <a href="/index.html" class="btn btn-success">首页</a>
            <a href="/add.html" class="btn btn-warning">添加</a>
            <br>
            <br>
        </div>
        <div class="row">
            <table id="tb" class="table table-bordered table-condensed table-hover table-responsive text-center">
            </table>
        </div>
    </div>

</body>

</html>
<script src="./static/js/jquery.min.js"></script>
<script>
    //渲染首页页面
    $.ajax({
        url: "/index", //请求接口地址
        type: "get", //请求的方式
        data: {},
        success: function (res) {
            //console.log(res)
            let str = "";
            str += `<tr>
                    <th>ID</th>
                    <th>姓名</th>
                    <th>性别</th>
                    <th>年龄</th>
                    <th>QQ</th>
                    <th>操作</th>
                </tr>`
            res.data.forEach((v) => {
                str += `<tr>
                    <td>${v.id}</td>
                    <td>${v.name}</td>
                    <td>${v.sex}</td>
                    <td>${v.age}</td>
                    <td>${v.qq}</td>
                    <td>
                        <a href="javascript:;" class="btn btn-danger" cid=${v.id}>删除</a>
                        <a href="javascript:;" class="btn btn-warning" cid=${v.id}>修改</a>
                    </td>
                </tr>`
            })
            //更新数据
            $("#tb").html(str);
        }
    })
    //删除数据
    //获取id值
    $("#tb").on("click",'a:nth-child(1)', function () {
        let id = $(this).attr("cid");
        // console.log(id);
        $.ajax({
            url: "/del", //请求接口地址
            type: "get", //请求的方式
            data: {
                id: id
            },
            success: function (res) {
                console.log(res);
            }
        })
    })

    $("#tb").on("click",'a:nth-child(2)', function () {
        let id = $(this).attr("cid");
        location.href=`edit.html?id=${id}`
    })
  
</script>